//
//  MAPYScanViewController.swift
//  Mars Proxy
//
//  Created by PowerfullClean on 2023/10/16.
//

import UIKit

class MAPYScanViewController: MAPYBaseViewController {
    init(mapy_funcType: MAPYFuncType = .mapy_speed) {
        super.init(nibName: nil, bundle: nil)
        self.mapy_funcType = mapy_funcType
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(mapy_navView)
        view.addSubview(mapy_loadingView)
        view.addSubview(mapy_iconView)
        view.addSubview(mapy_titleLabel)
        mapy_loadingView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 270, height: 276))
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        mapy_iconView.snp.makeConstraints { make in
            make.size.equalTo(146)
            make.centerX.centerY.equalTo(mapy_loadingView)
        }
        mapy_titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(mapy_loadingView.snp.bottom).offset(52)
        }
//        mapy_startScanTimer()
        MAPYManager.mapy_manager.mapy_delegate = self
        if mapy_funcType == .mapy_ip {
            mapy_iconView.image = UIImage(named: "mapy_scan_ip")
            MAPYManager.mapy_manager.mapy_requestIPInfoModel()
            mapy_titleLabel.text = "Speeding up testing your IP.\nThis process takes a few seconds"
           
        }else {
            MAPYManager.mapy_manager.mapy_startSpeedScan()
            mapy_speedValueModell = MAPYSpeedValueModel()
        }
        mapy_navView.mapy_titleLabel.text = mapy_funcType.mapy_title
    }
    
    // MARK: - mapy action
    
    // MARK: - mapy lazy
    var mapy_funcType: MAPYFuncType = .mapy_speed
    override func mapy_baseBgName() -> String? {
        return "mapy_scan_bg"
    }
    lazy var mapy_navView: MAPYNavView = {
        let mapy_navView = MAPYNavView()
        mapy_navView.mapy_leftBlock = {[weak self] in
            MAPYManager.mapy_manager.mapy_delegate = nil
            MAPYManager.mapy_manager.mapy_stopSpeedScan()
            self?.navigationController?.popToRootViewController(animated: true)
        }
        return mapy_navView
    }()
    lazy var mapy_loadingView: UIImageView = {
        let mapy_loadingView = UIImageView(image: UIImage(named: "mapy_scan_loading"))
        mapy_loadingView.layer.add(mapy_loadingKey, forKey: "mapy_loadingKey")
        return mapy_loadingView
    }()
    lazy var mapy_loadingKey: CABasicAnimation = {
        let mapy_loadingKey = CABasicAnimation.init(keyPath: "transform.rotation")
        mapy_loadingKey.fromValue = NSNumber.init(value: 0)
        mapy_loadingKey.toValue = NSNumber.init(value: Double.pi*2)
        mapy_loadingKey.duration = 1
        mapy_loadingKey.repeatCount = MAXFLOAT
        mapy_loadingKey.isRemovedOnCompletion = false
        return mapy_loadingKey
    }()
    lazy var mapy_iconView: UIImageView = {
        let mapy_iconView = UIImageView(image: UIImage(named: "mapy_scan_speed"))
        return mapy_iconView
    }()
    lazy var mapy_titleLabel: UILabel = {
        let mapy_titleLabel = UILabel()
        mapy_titleLabel.font = UIFont.systemFont(ofSize: 16)
        mapy_titleLabel.textColor = MAPYCommon.mapy_getHXColorValue(rgb: 0xFFFFFF)
        mapy_titleLabel.text = "Accelerating the testing of your network,\nthis process takes a few seconds."
        mapy_titleLabel.numberOfLines = 0
        mapy_titleLabel.textAlignment = .center
        return mapy_titleLabel
    }()
    var mapy_scanName: String = "mapy_scanName"
    var mapy_scanCount = 0
    var mapy_speedValueModell: MAPYSpeedValueModel?
}
extension MAPYScanViewController {
    fileprivate func mapy_startScanTimer() {
        mapy_scanName = MAPYTimerCommon.mapy_timeTask(self, selector: #selector(mapy_scanSum), start: 0, interval: 1, repeats: true, async: false, name: mapy_scanName)
    }
    
    fileprivate func mapy_stopScanTimer() {
        MAPYTimerCommon.mapy_cancelTask(mapy_scanName)
        mapy_scanName = "mapy_scanName"
        mapy_scanCount = 0
    }
    
    @objc fileprivate func mapy_scanSum() {
        mapy_scanCount = mapy_scanCount + 1
        mapy_stopScanTimer()
    }
    
}

extension MAPYScanViewController: MAPYManagerDelegate {
    func mapy_iPScanFinish(mapy_model: MAPYIPScanModel?) {
        MAPYManager.mapy_manager.mapy_delegate = nil
        navigationController?.pushViewController(MAPYTestResultViewController(mapy_funcType: .mapy_ip,  mapy_resultModel: mapy_model), animated: true)
        NotificationCenter.default.removeObserver(self)
    }
    func mapy_upLoadFinish(mapy_value: String) {
        mapy_speedValueModell?.mapy_upload = mapy_value
    }
    func mapy_downloadFinish(mapy_value: String) {
        mapy_speedValueModell?.mapy_download = mapy_value
        
    }
    func mapy_pingFinish(mapy_ping: String, mapy_jitter: String, mapy_loss: String) {
        mapy_speedValueModell?.mapy_loss = mapy_loss
        mapy_speedValueModell?.mapy_jitter = mapy_jitter
        mapy_speedValueModell?.mapy_ping = mapy_ping
        navigationController?.pushViewController(MAPYTestResultViewController(mapy_funcType: .mapy_speed,  mapy_resultModel: mapy_speedValueModell), animated: true)
        NotificationCenter.default.removeObserver(self)
    }
}
